//
//  RoomView.m
//  GKBHouse
//
//  Created by  on 12-6-22.
//  Copyright (c) 2012年 __MyCompanyName__. All rights reserved.
//

#import "RoomView.h"
#import "GKBNavigationBar.h"
#import "GKBMainController.h"
@implementation RoomView
@synthesize room;
@synthesize target;
@synthesize action;
- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        // Initialization code
        
    }
    return self;
}
- (void)removeFromSuperview
{
    [super removeFromSuperview];
    [devicesScrollView release];
}
- (id)init
{
    self = [super init];
    if (self) {
        // Initialization code
        [self initTitle];
        [self initDeviceFrame];
    }
    return self;
}
- (id)initWithRoom:(HouseButton*)house
{
  //  self = [super initWithFrame:CGRectMake(0, 0, 480, 320)];
    
    self = [super init];
    if (self) {
        // Initialization code
        self.room = house;
        [self initTitle];
        [self initDeviceFrame];
        [self initDevices];
    }
    return self;
    
}
- (void)roomView:(HouseButton*)house
{
    self.room = house;
    [self initDevices];
}
- (void)roomViewWithDevices:(NSMutableArray *)adevices
{
    devices = adevices;
    [self initDevices2];
}
- (void)setTarget:(id)aTarget action:(SEL)a
{
    self.target = aTarget;
    self.action = a;
}
- (void)initTitle
{
    GKBNavigationBar *topBar = [[GKBNavigationBar alloc]init];
    [topBar setFrame:CGRectMake(-kHouseMenuWidth, 0, DEVICE_HEIGHT, 30)];
   // [topBar setTarget:self returnAction:@selector(closeView) withTitle:self.room.name];
    topBar.refreshButton.hidden = YES;
    [self addSubview:topBar];
    
   // topBar.refreshButton.hidden = NO;
    
    [topBar release];

}
- (void)initDeviceFrame
{
    [self setFrame:CGRectMake(kHouseMenuWidth, 0, kDeviceMenuWidth, 320)];
    
    CGFloat height = DEVICE_WIDTH - kTopBarHeight;
    
    devicesScrollView = [[UIScrollView alloc]initWithFrame:CGRectMake(0, kTopBarHeight, kDeviceMenuWidth, height)];

    [self addSubview:devicesScrollView];
}
- (void)initDevices2
{
    for (UIView *sub in [devicesScrollView subviews]) {
        [sub removeFromSuperview];
    }
    
    NSInteger count = [devices count];
    if (count == 0) {
        return;
    } 

    NSInteger line = (count - 1)/5 + 1;
    if (line <= 0) {
        line = 0;
    }
    UIView *view = [[UIView alloc]init];
    for (int i = 0; i<line; i++) {//行
        for (int j = 0; j<5; j++) {//每行不超过5个
            int k = i * 5 + j;
            CGRect rect = CGRectMake(2 + j*kViewWidth, 10 + i*kViewHeight, kViewWidth, kViewHeight);
            
            NewEquipment *equipment = [devices objectAtIndex:k];
            DeviceView *device = [[DeviceView alloc]initWithFrame:rect withEqu:equipment]; 
            BOOL open = NO;
            if ([equipment.state isEqualToString:@"1"]) {
                open = YES;
            }
            if ([equipment.state isEqualToString:@"2"]) {
                open = NO;
            }
            [device setDeviceOn:open];
            [view addSubview:device];
            [device release];
            if ((k + 1) == count) {
                break;
            }
            
        } 
    }
    
    [view setFrame:CGRectMake(0, 0, kDeviceMenuWidth, 320 - 30)];
    [devicesScrollView addSubview:view];
    [view release];
}
- (void)initDevices
{
    NSInteger count = [room.devices count];
    if (count == 0) {
        return;
    }
    //  UIScrollView *devicesScrollView = [[UIScrollView alloc]initWithFrame:CGRectMake(kHouseMenuWidth, 30, kDeviceMenuWidth, 290)]; 
    for (UIView *sub in [devicesScrollView subviews]) {
        [sub removeFromSuperview];
    }
    
    NSInteger startX = 15;
    NSInteger rowMax = (kDeviceMenuWidth - startX*2)/kViewWidth;//每行的可显示最大个数
    
    NSInteger line = (count - 1)/rowMax + 1;
    if (line <= 0) {
        line = 0;
    }
    //UIView *view = [[UIView alloc]init];
    for (int i = 0; i<line; i++) {//行
        for (int j = 0; j<rowMax; j++) {//每行不超过每行的最大个数
            int k = i * rowMax + j;
            CGRect rect = CGRectMake(startX + j*kViewWidth, 10 + i*kViewHeight, kViewWidth, kViewHeight);
            
            NewEquipment *equipment = [room.devices objectAtIndex:k];
            DeviceView *device = [[DeviceView alloc]initWithFrame:rect withEqu:equipment]; 
            BOOL open = NO;
            if ([equipment.state isEqualToString:@"1"]) {
                open = YES;
            }
            if ([equipment.state isEqualToString:@"2"]) {
                open = NO;
            }
            [device setDeviceOn:open];
            [devicesScrollView addSubview:device];
            [device release];
            if ((k + 1) == count) {
                break;
            }
            
        } 
    }
    [devicesScrollView setContentSize:CGSizeMake(kDeviceMenuWidth, 10 + kViewHeight*line)];
   // [view setFrame:CGRectMake(0, 0, kDeviceMenuWidth, 320 - 30)];
  //  [devicesScrollView addSubview:view];
  //  [view release];
}
- (void)closeView
{
    if (self.target && self.action) {
        [self.target performSelector:self.action withObject:nil];
    }
 //   [self removeFromSuperview];
}
/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect
{
    // Drawing code
}
*/
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
    NSLog(@"touch");
   // [self closeView];
}
@end
